#include "math.h"
#define pi 3.1415926535
#define abs(a) ((a)>=(0)?(a):((a*(-1))))

double exp(double* xx)
{
	double x;
	x = *xx;
	int i,k,m,t;
	int xm=(int)x; 
	double sum;
	double e ;
	double ef;
	double z ;
	double sub=x-xm;
	m=1;      //阶乘算法分母
	e=1.0;  //e的xm
	ef=1.0;  
	t=10;      //算法精度
	z=1;  //分子初始化
	sum=1;   
	if (xm<0)
	{     //判断xm是否大于0？
   		xm=(-xm);
   		for(k=0;k<xm;k++){ef*=2.718281;}
		e/=ef;
	}
	else 
	{ 
		for(k=0;k<xm;k++)
		{
			e*=2.718281;
		}
	}
	for(i=1;i<t;i++)
	{
		m*=i;
		z*=sub;  
		sum+=z/m;
 	}
	*xx = sum*e;
	return sum*e;
}

double sqrt(double* xx)
{
//        double xhalf = 0.5f*x;
//        int i = *(int*)&x;
//        i = 0x5f3759df - (i >> 1);        // 计算第一个近似根
//        x = *(double*)&i;
//        x = x*(1.5f - xhalf*x*x);       // 牛顿迭代法
//        return x;
	double x;
	double x0;
	double x1;
	x1 = 1.0;
	x = *xx;
	do
	{
		x0 = x1;
		x1 = (x1+x/x1)/2;
	}while (!((x1-x0>-0.0000001) && (x1-x0<0.0000001)));
	*xx = x1;
	return x1*1.0;
}

double fabs(double* xx)
{
	double x;
	x = *xx;
	if (x>=0) 
	{
	}
	else
	{
		x = 0 - x;
	}

	*xx = x;
	return x;
}

double sin(double* xx)
{
	double x;
	double y;
	x = *xx;
	while (x<pi*(-1))
		x += pi*2;
	while (x>pi)
		x -= pi*2;
	if (x<0) 
	{
		y = 1.27323954 * x + 0.405284735 * x * x;
		if (y<0)
		{
			y = 0.225*(y*y*(-1)-y) + y;
		}
		else
		{
			y = 0.225*(y*y-y)+y;
		}
	}
	else
	{
		y = 1.27323954 * x - 0.405284735 * x * x;
		if (y<0)
		{
			y = 0.225 * (y*y*(-1)-y)+y;
		}
		else
		{
			y = 0.225*(y*y-y)+y;
		}
	}
	*xx = y;
	return *xx;
}
double sine(double* xx)
{
	double x = *xx;
	while (x<pi*(-1))
		x += pi*2;
	while (x>pi)
		x -= pi*2;
	const double B = 4/pi;
	const double C = -4/(pi*pi);
	double y = B * x + C * x * abs(x);
	const double P = 0.225;
	y = P * (y*abs(y) - y) + y;
	*xx = y;
}

double pow(double x,int n)
{
	int i=0;
	double dResult;
	dResult = 1.0;
	for (i=1;i<=n;i++)
	{
		dResult *= x;
	}
	return dResult;
}
double cos(double* xx)
{
	double x;
	x = *xx;
	double y;
	x = x + pi/2;
	y = sine(&x);
	*xx = x;
	return 0;
}

double ln(double* xx)
{
	double x,result;
	x = *xx;
	double e = 2.71828182845904523536;
	double ESP = 1e-20;
	double resutl;
	long k,i;
	k = 0;
	while (x/e>1)
	{
		x /= e;
		k++;
	}
	while (x*e<=1)
	{
		x *= e;
		k--;
	}	
	for (x-=1,result=0,i=1;x/i>ESP;)
	{
		result += x/i++;
		result -= x/i++;
	}
	*xx = result+k;
	return x;
}

double power(double* xx,double* yy)
{
	double result; 
	double x,y;
	result = pow_i(*xx,(int)*yy);
	*xx = result;
	return ;
}

double pow_i(double num,int n)
{
	double powint = 1;
	int i;
	for(i=1;i<=n;i++) 
		powint*=num;
	return powint;
}

double pow_f(double num,double m)
{
	int i,j;
	double powf,x,tmpm;
	powf = 0;
	tmpm = 1;
	x = num - 1;
	for (i=1;tmpm>1e-12 || tmpm<-1e-12;i++)
	{
		for(j=1,tmpm=1;j<=i;j++)
		{
			tmpm*= (m-j+1)*x/j;	
			powf+=tmpm;
		}
	}	
	return powf+1;
}

